<template>
	<div>
		<button style="display: block;float: left;width: 20vw;font-size: 5px;padding: 0px;margin-top: 16px;background-color: transparent;">商品名字</button>
		<input style="display: block;float: left;width: 60vw;" type="text" v-model="title" class="title" placeholder="请输入名称">
		<button style="display: block;float: left;width: 20vw;font-size: 5px;padding: 0px;margin-top: 16px;background-color: transparent;">商品单价</button>
		<input style="display: block;float: left;width: 60vw;" type="number" v-model="price" class="title" placeholder="请输入价格,单位元">
		<button style="display: block;float: left;width: 20vw;font-size: 5px;padding: 0px;margin-top: 16px;background-color: transparent;">商品描述</button>
		<input style="display: block;float: left;width: 60vw;" type="text" v-model="goodsDes" class="title" placeholder="请输商品描述">
		<button style="display: block;float: left;width: 20vw;font-size: 5px;padding: 0px;margin-top: 16px;background-color: transparent;">商品库存</button>
		<input style="display: block;float: left;width: 60vw;" type="number" v-model="stock" class="title" placeholder="当前库存">
		<!-- <textarea class="textarea" placeholder="请在此输入内容" v-model="text"></textarea> -->
		<!-- <image class="timg" src="http://rb1oc9tg0.hn-bkt.clouddn.com/dewu/blog/2022-05-08/20:31:46u51hh9.png" alt="123"></image> -->
		<image class="timg" :src="picture" v-if="picture != null"></image>
		
		<button @click="upload()" style="width: 30vw;font-size: 10px;float: right;">添加图片</button>
		<view style="clear: both;"></view>
		
		<radio-group @change="change" style="display: block;  margin: 50px auto;float: left;">
			<p style="display: block;margin: 20px;">选择商品类型</p>
			<view v-for="item in goodtype" v-if="goodtype != null">
				<radio :id="item.id" :value="item.id"></radio>
				<label :for="item.id" >{{item.name}}</label>
			</view>
		</radio-group>
		
		<radio-group @change="changet" style="display: block;  margin: 50px auto;float: left;">
			<p style="display: block;margin: 20px;">特价商品</p>
				<radio id="t" value="1"></radio>
				<label for="t" >特价</label>
				
				<radio id="f" value="0"></radio>
				<label for="f" >非特价</label>
		</radio-group>
		<view style="clear: both;"></view>
		
		
		<button class="buyRed" @click="buyred()">购买热门</button>
		<input class="buyresinput" v-show="showred" type="number" v-model="buy" placeholder="请输入要购买的金额" >
		<view style="clear: both;"></view>
		<button @click="send()">发布</button>
	</div>
</template>

<script>
	var picture = "null"
	var goodtype = null
	var typeId = 0
	var t = 0
	var showred = false
	// console.log("http://" + picture.slice(50,-29))

	export default{
		name:"addGood",
		data(){
			return{
				picture:picture,
				title:"",
				goodtype:goodtype,
				typeId:typeId,
				price:"",
				goodsDes:"",
				t:t,
				stock:"",
				buy:"",
				showred:showred
			}
		},
		onLoad:function(){
			this.picture = "null"
			uni.request({
				url: "http://" + getApp().globalData.IpAddr + "/goods-type",
				method:"GET",
				header:{	'authorization':getApp().globalData.token	},
				success:res=> {
					if(!res.data.success) wx.showToast({	title: '发送请求失败',	duration: 2000	})
					else {
						getApp().globalData.GoodsType = res.data.data
						this.goodtype = getApp().globalData.GoodsType
						// console.log(getApp().globalData.GoodsType)
					}
				}
			})
		},
		methods:{
			buyred(){
				this.showred = !this.showred
			},
			changet(e){
				this.t = e.detail.value
				console.log(this.t)
			},
			change(e){
				this.typeId = e.detail.value
				console.log(this.typeId)
			},
			upload(){
				
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'http://'+ getApp().globalData.IpAddr + '/upload/blog',
							filePath: tempFilePaths[0],
							success: res => {
								// console.log(res)
								// console.log("http://" + JSON.stringify(res).slice(55,-30))
								picture = "http://" + JSON.stringify(res).slice(55,-30)
								this.picture = "http://" + JSON.stringify(res).slice(55,-30)
							},
							fail:res=>{
								console.log(res)
							}
							
						});	
						
						
					}
				});
			},
			send(){
				// if(this.stock < 50) wx.showToast()
				// console.log(this.picture)
				// title.:"",
				// goodtype:goodtype,
				// typeId:typeId,
				// price:"",
				// goodsDes:"",
				// t:t,
				// stock:"",
				// buy:"",
				
				if(this.t.length == 0||this.title.length == 0 || this.price.length == 0 || this.goodsDes.length == 0 || this.stock.length == 0){
					wx.showToast({	title: "内容不能为空",	duration: 2000	})
				}else{
					uni.request({
						url:'http://'+ getApp().globalData.IpAddr + '/goods/save',
						method:"POST",
						header:{
							'authorization' : getApp().globalData.token
						},
						data:{
							'name' : this.title,
							'typeId' :this.typeId,
							'images' : this.picture,
							'price' : this.price,
							'goodsDes' : this.goodsDes,
							'type' : this.t,
							'area' : "1324567",
							'address' : "1234567",
							'stock' : this.stock,
							'isMoney' : this.buy
						},
						success:res=>{
							console.log(res)
							if(!res.data.success) wx.showToast({	title: res.data.errorMsg,	duration: 2000	})
							else {
								wx.showToast({	title: '上传成功',	duration: 2000	})
								
							}
						}
					})
				}
			}
		}
	}
</script>

<style>
	button::after{
		border: none;
	}
	.buyresinput{
		width: 50vw;
		float: left;
		height: 40px;
	}
	.buyRed{
		width: max-content;
		float: left;
		height: 40px;
	}
	.title{
		/* border:1px solid red; */
		margin: 5vw;
		width: 90vw;
		border-radius: 10px;
	}
	.textarea{
		/* border:1px solid red; */
		margin: 5vw;
		width: 90vw;
		border-radius: 10px;
	}
	.avc{
		/* background-color: red; */
		width: 10vw;
		height: 10vw;
	}
	.timg{
		height: 30vw;
		width: 30vw;
	}
</style>